<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>云校通-教师-晚修辅导登记</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<body ontouchstart style="background:#fff;" class="js-jf">
	<div class="weui_tab tab-bottom">
      	<div class="weui-header bg-blue">
	  		<div class="weui-header-left"> <a onclick="back()" href="javascript:" class="icon icon-109 f-white">返回</a> </div>
	    	<h1 class="weui-header-title">${weekText } ${coachTypeText } 辅导登记</h1>
		</div>
		<div class="weui_tab_bd">
			<div class="weui_grids" id="planList">
				
			</div>
		</div>
    </div>
    
    <div class="hide_bd"></div>
	<div class="hb_choose type1">
		<h1>请选择教师</h1>   
		<div class="searchbar_wrap"></div>
		<div class="name_show">
			<ul></ul>
		</div>
		<div class="name_show_box">
			<ul>
			</ul>
		</div>
		<input type="hidden" id="type"/>
		<div class="bg_white last_li">
			<strong><b class="cancel">取消</b></strong>
			<strong><b class="bg-blue f-white" id="sure">确认</b></strong>
	    </div>
	</div>
</body>
<script type="text/javascript">
	var menukey;
	$(function() {
		initPlan();
		$('.searchbar_wrap').searchBar({
	        cancelText:"取消",
	        searchText:'请输入教师名称',
	        onfocus: function (value) {
	           $('.name_show_box').show();
	        },
	        onblur:function(value) {
	        		
	        },
	        input: function(value) {
	        	queryItem(value);
	        },
	        onsubmit:function(value){
	        	queryItem(value);
	        },
	        oncancel:function(){
	        	$('.name_show_box ul').html('');
	        	$('.name_show_box').hide();
	        },
	        onclear:function(){
	        	$('.name_show_box ul').html('');
	        	$('.name_show_box').hide();
	        }
	    });
		$('.last_li .cancel').click(function(){
    		$('.hide_bd').attr("style", "display: none");
    		$('.type1').hide();
    	});
		$(".type1 #sure").click(function() {
	   		var teachers = "";
	   		var length = $(".name_show").find("li").length;
	   		if(length == 0) return;
	   		$(".name_show").find("li").each(function(i, v) {
	   			teachers += $(v).attr("data-name") + ((i+1) < length ? "," : "");
	   		})
	   		var obj = {
				week: "${week}",
				classId: $("#type").val(),
				coachType: "${coachType}",
				teacherName: teachers
			}
			addReg(obj);
	   		$($(".name_show").find("ul")).html('');
	   		$('.hide_bd').attr("style", "display: none");
	 		$('.type1').hide();
	   	})
	})
	
	function gridAdd(thiz) {
 		$('.hide_bd').attr("style", "display: block");
 		$('.type1').show();
 		$("#type").val($(thiz).attr("data-id"));
 	}
	
	var listMap = new Map();
	function initPlan() {
		$.ajax({
			url: basePath + "/ls/late/coach/getLateCoachPlan",
			type: 'POST',
			dataType : "json",
			data: {
				week: "${week}",
				coachType: "${coachType}"
			},
			success: function(rspData) {
				var html = '';
		    	var umap = new Map();
				for(var i in rspData) {
					listMap.set(rspData[i].id, rspData[i]);
					if(rspData[i].classId < 200) {
						var key = "初一";
						if(umap.get(key) == null) {
							if(!menukey) menukey = key;
							umap.set("初一", []);
						}
						umap.get("初一").push(rspData[i]);
					} else if(rspData[i].classId < 300) {
						var key = "初二";
						if(umap.get(key) == null) umap.set("初二", []);
						umap.get("初二").push(rspData[i]);
					} else if(rspData[i].classId < 400) {
						var key = "初三";
						if(umap.get(key) == null) umap.set("初三", []);
						umap.get("初三").push(rspData[i]);
					}
				}
				umap.forEach(function(value, key){
					html += loadHtml(key, value);
				});
				$("#planList").html(html);
			},
			error: function(data) {
				$.alert("系统异常,请联系管理员", "");
			}
		});
	}
	
	function loadHtml(key, rspData) {
		var html = '<div class="weui_panel">\
	        <div class="weui_panel_bd">\
	            <div class="weui_media_box weui_media_small_appmsg">\
	                <div class="weui_cells weui_cells_access">\
	                    <a href="javascript:;" class="weui_cell bl_d9"  onclick="selectmenu(\''+key+'\');">\
	                        <div class="weui_cell_hd"><i class="icon icon-2 f20"></i></div>\
	                        <div class="weui_cell_bd weui_cell_primary">\
	                            <p>'+key+'</p>\
	                        </div>\
	                        <span id="cell_1" class="icon icon-74 f-green"></span>\
	                    </a>\
	                </div>\
	            </div>\
	        </div>';
	        if(key == menukey) html += '<div id="menu_'+key+'">';
	        else html += '<div style="display: none;" id="menu_'+key+'">';
	        for(var i in rspData) {
				html += '<div class="weui_grid_w50">\
					<div class="weui_cell padding0">\
						<div class="weui_cell_hd p5">'+rspData[i].classId+'班</div>\
							<div class="weui_cell_bd weui_cell_primary padding0 height112 bl_d9_left">';
					var planCheck = false;
					for(var j in rspData[i].regList) {
						var itm = rspData[i].regList[j];
						if(itm.id) {
							html += '<p class="grid_p">'+itm.teacherName+' <label data-id="'+rspData[i].id+'" data-name="'+itm.teacherName+'" class="check_label" onclick="operate(this)"><span class="weui_icon_checked"></span></label></p>';
							if(itm.teacherName == rspData[i].teacherName) {
								planCheck = true;
							}
						}
					}
					if(!planCheck) {
						html += '<p class="grid_p">'+rspData[i].teacherName+' <label data-id="'+rspData[i].id+'" data-name="'+rspData[i].teacherName+'" class="nocheck_label" onclick="operate(this)"><span class="weui_icon_checked"></span></label></p>';
					}
				if(rspData[i].regList.length < 3) {
					html += '<p onclick="gridAdd(this)" class="grid_add" data-id="'+rspData[i].classId+'">+</p>';
				}
				html += '</div></div></div>';
			}
	      html += '</div></div>';
	      return html;
	}
	
	function operate(thiz) {
		var teacher = $(thiz).attr("data-name");
		var planId = $(thiz).attr("data-id");
		var data = listMap.get(parseInt(planId));
		if($(thiz).hasClass("nocheck_label")) {
			var obj = {
				week: "${week}",
				classId: data.classId,
				coachType: "${coachType}",
				teacherName: teacher
			}
			addReg(obj);
		}else {
			for(var i in data.regList) {
				var reg = data.regList[i];
				if(reg.teacherName == teacher && reg.id) {
					cancelReg(reg.id);
					break;
				}
			}
		}
	}
	
	function addReg(data) {
		$.ajax({
			url: basePath + "/ls/late/coach/addReg",
			type: 'POST',
			dataType : "json",
			data: data,
			success: function(resp) {
				if(resp.code == '000') {
					if(resp.data) {
						$.alert(resp.data, "");
					}else {
						//$.toast("操作成功");
					}
					initPlan();
				}else {
					$.toast(resp.msg, "cancel");
				}
			},
			error: function(data) {
				$.alert("系统异常,请联系管理员", "");
			}
		});
	}
	function cancelReg(id) {
		$.ajax({
			url: basePath + "/ls/late/coach/cancelReg",
			type: 'POST',
			dataType : "json",
			data: {
				id: id
			},
			success: function(resp) {
				if(resp.code == '000') {
					//$.toast("操作成功");
					initPlan();
				}else {
					$.toast(resp.msg, "cancel");
				}
			},
			error: function(data) {
				$.alert("系统异常,请联系管理员", "");
			}
		});
	}
	function listDel(thiz) {
    	$(thiz).remove();
    }
    function searchPoint(thiz) {
    	var teacherId = $(thiz).attr("data-id");
    	var teacherName = $(thiz).attr("data-name");
    	$('.name_show_box ul').html('');
    	$('.name_show_box').hide();
    	$("#weui_search_input").val(null);
   		$('.name_show').find('ul').append('<li onclick="listDel(this)" class="list-del" data-id="'+teacherId+'" data-name="'+teacherName+'">'+teacherName+'<span class="list-del-vaild"></span></li>');
    }
    var search = false;
    function queryItem(keyword) {
 		if(!/^[\u4E00-\u9FA5]+$/.test(keyword)) return;
 		if(search) return;
 		search = true;
 		$.ajax({
     		url: basePath + "/ls/teacher/manage/keyword",
     		type: 'POST',
     		dataType : "json",
     		data: {
     			teacherName: keyword
     		},
     		success: function(data) {
     			search = false;
     			if (data != null && data.length > 0) {
     				var html = "";
     				for(var i in data) {
     					var itm = data[i];
     					html += "<li onclick='searchPoint(this)' data-id="+itm.id+" data-name="+itm.name+">"+itm.name+"</li>";
     				}
     				$(".name_show_box ul").html(html);
     			}
     		},
     		error: function(data) {
     			alert("系统异常,请联系管理员");
     		}
     	});
 	}
    function selectmenu(n){
    	menukey = n;
    	var eleMore = document.getElementById("menu_"+n);
    	if(eleMore.style.display=="none"){
	    	eleMore.style.display = 'block';
	    	$("#cell_"+n).removeClass("icon-74");
	    	$("#cell_"+n).addClass("icon-35 ");
    	}else{
	    	eleMore.style.display = 'none';
	    	$("#cell_"+n).removeClass("icon-35");
	    	$("#cell_"+n).addClass("icon-74");
    	}
    }
</script>
</html>